import 'package:ali_auth/ali_auth.dart';
import 'package:get/utils.dart';

class AliLogin {
  static final AliLogin instance = AliLogin._interval();
  factory AliLogin() {
    return instance;
  }
  AliLogin._interval() {
    init();
  }
  init() {}

  Future<dynamic> register() async {
    /// 初始化插件
    /// 在使用参数时isDialog，请参照默认配置进行所需修改，否则可能出现相关问题
    /// 两个配置文件分别是全屏以及弹窗的配置参数
    /// 详情请点击进入查看具体配置
    var result;
    if (GetPlatform.isAndroid) {
      result = await AliAuthPlugin.initSdk(
        sk: 'uR/KqhZdUrv5b59sM95jxSQnBaQHFR6Nxnq8rU7AoniBYkUz31GOpF1e/WgOqlSsdmaJ4I7Ub03sftlSuv8N02+FQSyB5v4AtHD+qc4Sgkr/HUw8Z7g+L9+TvvZr/q+T1+vVuLVS27DEZkCXTfd+2AQMRE7329vMJtDjgkQPkAL5AjocpBHZo7iv6o1T00lFvnesj2etHn6JgRwD+u4PTsceGOT+8OUFvepLoonnXKioQ80qEjZdW2qiRKnly+CtJMPsp8G6ELfyR4VesX87iw==',
        config: getConfig(),
      );
    } else if (GetPlatform.isIOS) {
      result = await AliAuthPlugin.initSdk(
        sk: '6QzZRbemo+1Zm/C6pMyJQ34YZDafH0UCvIUN1hMnHYHnL5Be2MzeRRC2tmWUywNpxWLvqh9kjrRcE7lVVUZFTWCqKvp2VZuvOIOrZIfCxWWcu7YfEDCrlwekJhEh0EGot3tsDcO8pvgLY5nIc7FmiFGdaWDJ8j6mMRkQJ66PC82H5k9ZR8+MTdGC0zH13ToUxzRGP2d3vzNOAFUbzuKnJA6NdndsTb+CzzoBPR0n3pPuknIUI7u0V5rEc8x7D1pg1lALpB7TFi5y5di3vmUCz6iNk1oyj/9xqNmUHpJg8BY=',
        config: getConfig(), //getDislogConfig(),
      );
    }

    /// 登录监听
    AliAuthPlugin.loginListen(
        type: false, onEvent: _onEvent, onError: _onError);

    return result;
  }

  login() async {
    await register();
    AliAuthPlugin.login;
  }

  /// 登录成功处理
  void _onEvent(event) async {
    Get.log("-------------成功分割线------------$event");
    if (event != null && event['code'] != null) {
      if (event['code'] == '600024') {
        await AliAuthPlugin.login;
      } else if (event['code'] == '600000') {
        Get.log('获取到的token${event["data"]}');
      }
    }
  }

  /// 登录错误处理
  void _onError(error) {
    Get.log("-------------失败分割线------------$error");
  }
}
